const app = getApp();

Page({
  data: {
    city: '上海',
    healthTags: ['高血压', '冠心病', '如何控制血糖'],
    mainServices: [
      { 
        id: 1, 
        name: '家政护理', 
        desc: '快速上门服务',
        icon: 'https://dummyimage.com/120x120/8B9FE8/fff.png&text=家政',
        bgColor: 'linear-gradient(135deg, #8B9FE8 0%, #A8C0FF 100%)'
      },
      { 
        id: 2, 
        name: '康复理疗', 
        desc: '全套护理方案',
        icon: 'https://dummyimage.com/120x120/ED9FE8/fff.png&text=康复',
        bgColor: 'linear-gradient(135deg, #ED9FE8 0%, #FFC3F0 100%)'
      },
      { 
        id: 3, 
        name: '上门体检', 
        desc: '专业医师团队',
        icon: 'https://dummyimage.com/120x120/FF9F9F/fff.png&text=体检',
        bgColor: 'linear-gradient(135deg, #FF9F9F 0%, #FFC3C3 100%)'
      }
    ],
    functions: [
      { id: 1, name: '健康数据', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#E8F9F3' },
      { id: 2, name: '设备中心', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#E8F4FF' },
      { id: 3, name: '用药信息', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#FFF8E8' },
      { id: 4, name: '健康膳食', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#E8F4FF' },
      { id: 5, name: '疾病宝典', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#FFE8F0' },
      { id: 6, name: '健康档案', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#F0FFF4' },
      { id: 7, name: '老年活动', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#F0E8FF' },
      { id: 8, name: '健康资讯', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#FFF0E8' },
      { id: 9, name: '养老机构', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#E8FFFA' },
      { id: 10, name: '健康讲堂', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#FFE8E8' },
      { id: 11, name: '饮食记录', icon: 'https://dummyimage.com/100x100/eeeeee/aaaaaa.png&text=icon', bgColor: '#E8F8FF' }
    ],
    diseases: ['白内障', '高血压', '中风', '阿尔兹海默症', '糖尿病', '心率不齐'],
    newsList: [
      {
        id: 1,
        title: '老年人如何控制血糖？',
        desc: '控制血糖对于维持健康的生活方式和预防糖尿病等疾病至关重要。以下是一些控制血糖的方法：',
        images: [
          'https://dummyimage.com/400x300/FFB6C1/fff.png&text=图1',
          'https://dummyimage.com/400x300/87CEEB/fff.png&text=图2',
          'https://dummyimage.com/400x300/FFA07A/fff.png&text=图3'
        ],
        likes: 1001,
        collects: 210,
        comments: 6,
        liked: false,
        collected: false
      },
      {
        id: 2,
        title: '老年人如何控制血糖？',
        desc: '控制血糖对于维持健康的生活方式和预防糖尿病等疾病至关重要。以下是一些控制血糖的方法：',
        images: [
          'https://dummyimage.com/400x300/FFB6C1/fff.png&text=图1',
          'https://dummyimage.com/400x300/87CEEB/fff.png&text=图2',
          'https://dummyimage.com/400x300/FFA07A/fff.png&text=图3'
        ],
        likes: 856,
        collects: 189,
        comments: 12,
        liked: false,
        collected: false
      },
      {
        id: 3,
        title: '适合老年人的运动方式有哪些？',
        desc: '随着年龄增长，选择合适的运动方式对于保持健康至关重要。这里为您推荐几种适合老年人的运动。',
        images: [
          'https://dummyimage.com/400x300/98FB98/fff.png&text=太极',
          'https://dummyimage.com/400x300/DDA0DD/fff.png&text=散步',
          'https://dummyimage.com/400x300/F0E68C/fff.png&text=瑜伽'
        ],
        likes: 723,
        collects: 156,
        comments: 8,
        liked: false,
        collected: false
      }
    ]
  },

  onLoad() {
    // 页面加载
  },

  onShow() {
    // 页面显示时更新tabbar选中状态
    if (this.getTabBar && this.getTabBar()) {
      this.getTabBar().setData({ selected: 0 });
    }
  },

  // 位置点击
  onLocationTap() {
    wx.showToast({
      title: '选择城市',
      icon: 'none'
    });
  },

  // 扫码
  onScan() {
    wx.scanCode({
      success: (res) => {
        console.log('扫码结果：', res);
      }
    });
  },

  // 搜索点击
  onSearchTap() {
    wx.showToast({
      title: '搜索功能',
      icon: 'none'
    });
  },

  // 标签点击
  onTagTap(e) {
    const tag = e.currentTarget.dataset.tag;
    wx.showToast({
      title: `查看${tag}相关内容`,
      icon: 'none'
    });
  },

  // 主要服务点击
  onMainServiceTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/service/booking/booking?serviceId=${id}`
    });
  },

  // 功能点击
  onFunctionTap(e) {
    const id = e.currentTarget.dataset.id;
    const functionMap = {
      1: '/pages/health/health',
      6: '/pages/mine/health-record/health-record',
      7: '/pages/circle/circle'
    };
    
    if (functionMap[id]) {
      if (id === 7) {
        wx.switchTab({
          url: functionMap[id]
        });
      } else {
        wx.navigateTo({
          url: functionMap[id]
        });
      }
    } else {
      wx.showToast({
        title: '功能开发中',
        icon: 'none'
      });
    }
  },

  // 更多疾病
  onMoreDisease() {
    wx.showToast({
      title: '查看更多疾病',
      icon: 'none'
    });
  },

  // 疾病点击
  onDiseaseTap(e) {
    const disease = e.currentTarget.dataset.disease;
    wx.showToast({
      title: `查看${disease}详情`,
      icon: 'none'
    });
  },

  // 新闻详情
  onNewsDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/news/news-detail?id=${id}`
    });
  },

  // 分享
  onShare(e) {
    const id = e.currentTarget.dataset.id;
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  // 点赞
  onLike(e) {
    const id = e.currentTarget.dataset.id;
    const newsList = this.data.newsList;
    const index = newsList.findIndex(item => item.id === id);
    
    if (index !== -1) {
      const liked = !newsList[index].liked;
      newsList[index].liked = liked;
      newsList[index].likes = liked ? newsList[index].likes + 1 : newsList[index].likes - 1;
      
      this.setData({
        newsList: newsList
      });
      
      wx.showToast({
        title: liked ? '已点赞' : '已取消',
        icon: 'success'
      });
    }
  },

  // 收藏
  onCollect(e) {
    const id = e.currentTarget.dataset.id;
    const newsList = this.data.newsList;
    const index = newsList.findIndex(item => item.id === id);
    
    if (index !== -1) {
      const collected = !newsList[index].collected;
      newsList[index].collected = collected;
      newsList[index].collects = collected ? newsList[index].collects + 1 : newsList[index].collects - 1;
      
      this.setData({
        newsList: newsList
      });
      
      wx.showToast({
        title: collected ? '已收藏' : '已取消',
        icon: 'success'
      });
    }
  },

  // 评论
  onComment(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '评论功能',
      icon: 'none'
    });
  },

  // 分享
  onShareAppMessage() {
    return {
      title: '智慧养老，关爱长辈',
      path: '/pages/index/index'
    };
  }
})
